import type { JSX } from "react";
import groups from "./groups";
import type { Friend, Message } from "./types";

// 生成初始消息 - 加入表情包消息示例
export const generateInitialMessages = (): Message[] => {
  const group = groups[0];
  const messages: Message[] = [];

  messages.push({
    id: "m1",
    sender: group.members.find((m) => m.name === "英子")!,
    content: "今晚7点，体育馆2号场地，约吗？",
    time: "18:30",
    type: "text",
    isMine: false,
  });

  messages.push({
    id: "m2",
    sender: group.members.find((m) => m.name === "常安")!,
    content: "我参加！带两副拍子",
    time: "18:32",
    type: "text",
    isMine: false,
  });

  messages.push({
    id: "m3",
    sender: group.members.find((m) => m.name === "乐呗")!,
    content: (
      <div className="flex items-center">
        <span className="text-2xl mr-1">👌</span>
        <span>好的，我带球</span>
      </div>
    ),
    time: "18:33",
    type: "emoji",
    isMine: true,
  });

  messages.push({
    id: "m4",
    sender: group.members.find((m) => m.name === "毛竹")!,
    content: (
      <div className="flex items-center">
        <span>算我一个，刚买了新球鞋</span>
        <span className="text-2xl ml-1">💪🏻</span>
      </div>
    ),
    time: "18:35",
    type: "emoji",
    isMine: false,
  });

  messages.push({
    id: "m5",
    sender: group.members.find((m) => m.name === "袁枚")!,
    content: "我晚点到，留个位置",
    time: "18:40",
    type: "text",
    isMine: false,
  });

  return messages;
};

// 头像占位函数
export const generateAvatar = (name: string, color: string) => {
  return (
    <div className={`avatar placeholder ${color}`}>
      <div className="bg-neutral-focus text-neutral-content rounded-full w-10">
        <span className="text-lg">{name.charAt(0)}</span>
      </div>
    </div>
  );
};

const getRandomGroupMember = (excludeName: string): Friend => {
  const group = groups[0];
  const availableMembers = group.members.filter((m) => m.name !== excludeName);
  return availableMembers[Math.floor(Math.random() * availableMembers.length)];
};

// 群成员模拟发言，返回 Message
export function generateRandomGroupMessage(meName: string): Message {
  const member = getRandomGroupMember(meName);
  const textMessages = [
    "有人要一起打球吗？",
    "我到了，你们在哪个场地？",
    "打完球一起去吃饭吧",
    "今天状态不错啊",
    "明天还打吗？",
    "拍子谁借我用一下？",
    "今天的球打得太爽了！",
    "谁能帮忙带瓶水？",
    "周末有约球的吗？",
  ];

  const emojiMessages = [
    "一起来🏃‍♂️💨",
    "🎉🎉🎉",
    "💯",
    "🔥🔥🔥",
    "😂😂",
    "🥳🥳🥳",
    "👏👏👏",
  ];

  // 随机决定是普通文本还是带表情的消息
  const useEmoji = Math.random() > 0.7;

  let content: string | JSX.Element;
  if (useEmoji) {
    const emojiMessage =
      emojiMessages[Math.floor(Math.random() * emojiMessages.length)];
    content = (
      <div className="flex items-center">
        <span>{emojiMessage}</span>
      </div>
    );
  } else {
    content = textMessages[Math.floor(Math.random() * textMessages.length)];
  }

  return {
    id: `m${Date.now()}`,
    sender: member,
    content,
    time: new Date().toTimeString().substring(0, 5),
    type: useEmoji ? "emoji" : "text",
    isMine: false,
  };
}
